﻿@page "/AgvrturnPage"
@inject PickingServer pickingServer
@using System.ComponentModel
@using AntDesign.TableModels
@using ShanjinPDAUI.ModelEntity
@inject ModalService _modalService
@inject MessageService _message
<h3>退/下料</h3>
<div>
    <Row>
        <Col Span="24">请选择AGV</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@AGVS"
                AllowClear="true"
                ValueOnClear="@(0)"
                @bind-Value="@agvselet"
                ValueName="@nameof(AGV_Entity.Deviceid)"
                LabelName="@nameof(AGV_Entity.Remark)">
        </Select>
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">AGV退料</Button>
        </Col>
    </Row>
    <br>

    <Row>
        <Col Span="24">
            @*   <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creatnobaked">生箔下料，无需烘烤</Button>*@
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
            @*  <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creatnotobebaked">生箔下料，无可用烘箱</Button>*@
        </Col>
    </Row>
</div>

@code {
    ObservableCollection<AGV_Entity> AGVS { get; set; } = new ObservableCollection<AGV_Entity>();
    ObservableCollection<Machine_Entity> machines { get; set; } = new ObservableCollection<Machine_Entity>();

    int agvselet { get; set; } = 0;
    bool Flag { get; set; } = false;
    protected override async Task OnInitializedAsync()
    {
        var aGVS = await pickingServer.GetAGVs();
        var machiness = await pickingServer.GetMachine();
        if (aGVS != null)
        {
            AGVS.Clear();
            foreach (var item in aGVS)
            {
                AGVS.Add(item);
            }
        }
        if (machiness!=null)
        {
            machines.Clear();
            foreach (var item in machiness)
            {
                machines.Add(item);
            }
        }

        base.OnInitialized();
    }
    private async Task Creat()
    {
        _modalService.Confirm(new ConfirmOptions()
            {
                Title = "提示",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            Content = "是否提交？",
            OkText = "确认",
            OnOk = async (e) =>
            {
                var agv = AGVS.FirstOrDefault(x => x.Deviceid == agvselet);
                if (agv != null)
                {
                    if (agv.SourPostion != 0)
                    {
                        //立库领料
                        var b = await pickingServer.HostreturnAGVFromLK(agv);
                        await _message.Info(b);
                    }
                    else
                    {
                        //拆包间领料
                        if (agv.SourId != 0)
                        {
                            var unpks = await pickingServer.GetuNPK();
                            var unpk = unpks.FirstOrDefault(s=>s.Deviceid==agv.SourId);

                            var mac = machines.FirstOrDefault(x => x.Devid == agv.ToPostion);
                            if (mac != null)
                            {
                                if (unpk != null)
                                {
                                    var b = await pickingServer.HostreturnagvUnpak(agv, unpk, mac.MESName.Split(",").First());
                                    await _message.Info(b);
                                }
                                else
                                {
                                    await _message.Info("数据不匹配");

                                }
                            }
                            else
                            {
                                await _message.Info("没有匹配的设备");
                            
                            }
                          

                        }
                    }
                }

            },
            CancelText = "取消"
        });


    }
}

